/*
 * @Author: HuRrongChao zhuangpiao@qingmutec.com
 * @Date: 2024-10-09 14:16:43
 * @LastEditors: HuRrongChao zhuangpiao@qingmutec.com
 * @LastEditTime: 2024-10-11 17:54:37
 * @FilePath: \rc_-short-video\src\components\message\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { createApp } from 'vue'
import message from './message.vue'

let instances = []
let removeMsg = (msgid) => {
  let removeTop = 68
  for (let i = 0; i < instances.length; i++) {
    if (instances[i]._props.msgid === msgid) {
      instances.splice(i, 1)
      for (const instance of instances) {
        const instanceData = instance._instance.data
        let currentTop = instanceData.top
        instanceData.top = currentTop - removeTop
      }
    }
  }
}

let open = (options) => {
  // 实例化
  options.msgid = 'msg' + '_' + new Date().getTime()
  let instance = createApp({
    extends: message,
    data: {
      onClose(msgid) {
        removeMsg(msgid)
      }
    }
  }, options)
  instance.mount(document.createElement('div'))
  const instanceData = instance._instance.data
  const instanceEl = instance._container.childNodes[0]
  let nextTop = 80
  for (const instance of instances) {
    nextTop += instance._container.offsetHeight + 20
  }
  instanceData.top = nextTop
  instances.push(instance)
  // 把dom加入到文档中
  document.body.appendChild(instanceEl)
  instanceData.visible = true
}
export default {
  open
}